﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Text;
using System.Xml;
using System.Net;
using WLMCoreLibrary;
using MiWebSite_Core;

namespace MWS_AddDirectory.GeoBusqueda
{
    public partial class Default : System.Web.UI.Page
    {
        public dbConnector ConexionDB;
        public WebSiteMainConfiguration config;
        protected void Page_Load(object sender, EventArgs e)
        {

            config = WebSiteMainConfiguration.GetConfig();
            ConexionDB = new dbConnector(config.Connections["WebSite"].value);
            //MiWebSiteFunctions.GrantAccess(ref ConexionDB);
            if (!IsPostBack)
            {
                
            }

            if (Request["lat"] != null && Request["lon"] != null)
            {
                int totalRegistros = 10;
                double latitud = Convert.ToDouble(Request["lat"]);
                double longitud = Convert.ToDouble(Request["lon"]);
                double radio=Convert.ToDouble(Request["distancia"]);
                if (Request.QueryString["noRegistros"] != null)
                    totalRegistros = Convert.ToInt32(Request.QueryString["noRegistros"]);                
                AnuncioCollection anuncios = new AnuncioCollection(latitud,longitud,radio, totalRegistros, ref ConexionDB);
                Response.ContentType = "text/xml";
                Response.Write(WLMFunctions.SerializeObject(anuncios, true));
                anuncios = null;
                ConexionDB.CloseConnection();
                Response.End();
            }
            if (Request.QueryString["original"] != null)
            {
            }
            else
            {
                if (Request.QueryString["xml"] != null)
                {
                    GenerarXML();
                }
                else
                {
                    TransformarXML();
                }
            }
            ConexionDB.CloseConnection();
        }

        protected void GenerarXML()
        {
            MiWebSitePage pagina = new MiWebSitePage(ref ConexionDB);
            pagina.Title = "GEOBusqueda - Directorio de " + DeCurrentDomain.Ciudad;
            pagina.DomainName = DeCurrentDomain.DomainName;
            pagina.DomainName = DeCurrentDomain.DomainName;
            pagina.DomainID = DeCurrentDomain.IdentificadorDominio;
            XmlDocument xml = new XmlDocument();
            WebClient web = new WebClient();
            string webpage = web.DownloadString("http://" + CurrentDomain.DomainName + "/GeoBusqueda/?original=true");
            //xml.LoadXml("<MiWebSiteContent><Contenido><![CDATA[<div id='panelAltaAnuncio'>.</div> <script>$('#panelAltaAnuncio').load('/NuevoAnuncio/?original=true')</script> ]]></Contenido></MiWebSiteContent>");
            xml.LoadXml("<MiWebSiteContent><Contenido><![CDATA[" + webpage + " ]]></Contenido></MiWebSiteContent>");
            pagina.Contenido = xml;
            Response.ContentType = "text/xml";
            Response.Write(WLMFunctions.SerializeObject(pagina, true));
            Response.End();
        }

        protected void TransformarXML()
        {
            List<ParametroTransformacionXML> parametros = FuncionesDirectorio.GetTransformationParameters();

            if (DeCurrentDomain.SubDomain != "www")
            {
                parametros.Add(new ParametroTransformacionXML("subDomainSystemName", new Categoria(DeCurrentDomain.SubDomain, true, ref ConexionDB).nombreCategoria));
            }

            string urlPageXML = "http://" + DeCurrentDomain.SubDomain + "." + DeCurrentDomain.DomainName + "/GeoBusqueda/?xml=true";
            string HTMLPagina = WLMFunctions.TransformXML(urlPageXML, config.Settings["siteURL"].value + config.Settings["skinPath"].value + "index.xsl", parametros);
            Response.Write(HTMLPagina);
            Response.End();
        }



    }
}